package leetCodeEveryday.Test2404出现最频繁的偶数元素;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Solution {
    public int mostFrequentEven(int[] nums) {
        Map<Integer, Integer> hashMap = new HashMap<>();
        for (int num : nums) {
            if (isEven(num)) {
                hashMap.put(num, hashMap.getOrDefault(num, 0) + 1);
            }
        }

        int count = 0;
        int mostFrequentEvenNum = -1;
        Set<Map.Entry<Integer, Integer>> entries = hashMap.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            Integer num = entry.getKey();
            Integer value = entry.getValue();
            if (value > count) {
                count = value;
                mostFrequentEvenNum = num;
            } else if (value == count) {
                mostFrequentEvenNum = Math.min(mostFrequentEvenNum, num);
            }
        }

        return mostFrequentEvenNum;
    }

    public boolean isEven(int num) {
        if (num % 2 == 0) {
            return true;
        } else {
            return false;
        }
    }
}
